package com.sip.stream.utils.zfy.yuv;

import android.media.Image;
import com.gosuncn.zfyluancher.ZFYLauncherJNI;
import com.sip.stream.utils.ImageYUVBean;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class GSYUVTool {
    private static final String TAG = "GSYUVTool";
    private ICamera2ImageReader camera2ImageReader;
    private YUVToolDataSource dataSource;
    private Thread getDataFormImageThread;
    private Thread handlerThread;
    private BlockingDeque<Image> imageWaitDeque;
    protected AsyncYUVToolListener listener;
    private ICamera2ImageReader monoCamera2ImageReader;
    private INV12Changer monoNv12Change;
    private ICamera2ImageReader normalCamera2ImageReader;
    private INV12Changer normalNv12Change;
    private INV12Changer nv12Changer;
    private BlockingDeque<ImageYUVBean> waitDeque;

    public interface AsyncYUVToolListener {
        void imageFormatResult(byte[] bArr, long j);
    }

    public void setMono(boolean mono) {
    }

    public void setDataSource(YUVToolDataSource yUVToolDataSource) {
        this.dataSource = yUVToolDataSource;
    }

    protected GSYUVTool() {
    }

    public void async_YUV_420_888ToNV12(Image image, long j) {
        BlockingDeque<Image> blockingDeque = this.imageWaitDeque;
        if (blockingDeque == null || blockingDeque.offer(image)) {
            return;
        }
        image.close();
    }

    public void release() {
        this.listener = null;
        Thread thread = this.handlerThread;
        if (thread != null) {
            thread.interrupt();
            this.handlerThread = null;
        }
        Thread thread2 = this.getDataFormImageThread;
        if (thread2 != null) {
            thread2.interrupt();
            this.getDataFormImageThread = null;
        }
        YUVToolDataSource yUVToolDataSource = this.dataSource;
        if (yUVToolDataSource != null) {
            yUVToolDataSource.release();
            this.dataSource = null;
        }
    }

    public GSYUVTool(AsyncYUVToolListener asyncYUVToolListener, ICamera2ImageReader iCamera2ImageReader, INV12Changer iNV12Changer, ICamera2ImageReader iCamera2ImageReader2, INV12Changer iNV12Changer2) {
        this.listener = asyncYUVToolListener;
        setupThread();
        this.camera2ImageReader = iCamera2ImageReader;
        this.nv12Changer = iNV12Changer;
        this.normalCamera2ImageReader = iCamera2ImageReader;
        this.normalNv12Change = iNV12Changer;
        this.monoCamera2ImageReader = iCamera2ImageReader2;
        this.monoNv12Change = iNV12Changer2;
    }

    private INV12Changer getNv12Changer() {
        if (this.nv12Changer == null) {
            this.nv12Changer = new NV12NoChanger(0, 0);
        }
        return this.nv12Changer;
    }

    protected ImageYUVBean getDataFormImage(Image image) {
        ICamera2ImageReader iCamera2ImageReader = this.camera2ImageReader;
        if (iCamera2ImageReader != null) {
            return iCamera2ImageReader.getDataFormImage(image);
        }
        return null;
    }

    protected byte[] changeToNV12(ImageYUVBean imageYUVBean) {
        return getNv12Changer().changeToNV12(imageYUVBean);
    }

    private void setupThread() {
        this.waitDeque = new LinkedBlockingDeque(5);
        Thread thread = new Thread(new Runnable() {
            @Override
            public  void run() {
                GSYUVTool.this.m524xad9f5870();
            }
        });
        this.handlerThread = thread;
        thread.start();
        this.imageWaitDeque = new LinkedBlockingDeque(1);
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                GSYUVTool.this.m525xd3336171();
            }
        });
        this.getDataFormImageThread = thread2;
        thread2.start();
    }

    public void m525xd3336171() {
        while (!Thread.interrupted()) {
            try {
                Image take = this.imageWaitDeque.take();
                ImageYUVBean dataFormImage = getDataFormImage(take);
                take.close();
                BlockingDeque<ImageYUVBean> blockingDeque = this.waitDeque;
                if (blockingDeque != null && dataFormImage != null) {
                    blockingDeque.offer(dataFormImage);
                }
            } catch (InterruptedException unused) {
                return;
            }
        }
    }

    public void m524xad9f5870() {
        while (!Thread.interrupted()) {
            try {
                ImageYUVBean take = this.waitDeque.take();
                AsyncYUVToolListener asyncYUVToolListener = this.listener;
                if (asyncYUVToolListener != null) {
                    asyncYUVToolListener.imageFormatResult(changeToNV12(take), take.getImageTimestampus());
                } else {
                    recycle(take.getCameraData());
                }
            } catch (InterruptedException unused) {
                return;
            }
        }
    }

    public static byte[] copyNV12ToNV21(byte[] bArr, int i, int i2) {
        byte[] bArr2 = new byte[bArr.length];
        if (ZFYLauncherJNI.NV12ToNV21(bArr, bArr2, i, i2) == 0) {
            return bArr2;
        }
        return null;
    }

    public void recycle(byte[] bArr) {
        YUVToolDataSource yUVToolDataSource = this.dataSource;
        if (yUVToolDataSource != null) {
            yUVToolDataSource.recycle(bArr);
        }
    }
}
